package BIO.test05;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;

public class ServerReaderThread extends Thread{
    private Socket socket;

    public ServerReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //1.得到一个数据输入流读取客户端发送过来的数据
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            //2. 读取到客户端发送过来的文件类型
            String suffix = dis.readUTF();
            System.out.println("服务端成功接收到了"+suffix+"文件");
            //3. 定义字节输出管道,负责把客户端发送过来的数据写出去
            OutputStream os = new FileOutputStream("F:\\Code\\java\\project\\source-code\\IO\\src\\main\\resources\\" + UUID.randomUUID().toString() + suffix);
            //4.从数据输入流中读取文件数据，将其写出到字节输出流中去保存
            byte[] buffer = new byte[1024];
            int len;
            while ((len = dis.read(buffer)) > 0){
                os.write(buffer,0,len);
            }
            os.close();
            System.out.println("服务端保存成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
